﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;

namespace BOH.ControlPanel
{
    public partial class UnitList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGovern();
                UnitListBind(drpG.SelectedValue.ToString());
            }
        }

        public void BindGovern()
        {
            DataSet dstGovern = UnitClass.getGovern();
            drpG.DataSource = dstGovern.Tables[0].DefaultView;
            drpG.DataTextField = "GovernName";
            drpG.DataValueField = "GovernID";
            drpG.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpG.Items.Add(listitem);
        }
        public void UnitListBind(string GovernID)
        {
            DataSet dst = UnitClass.getUnitByGovern(GovernID,true);
            BindUnitList(dst);
        }

        public void UnitListBind(string UnitName, string GovernID)
        {
            DataSet dst = UnitClass.getUintByGovern(UnitName, GovernID);
            BindUnitList(dst);
        }

        public void BindUnitList(DataSet dst)
        {
            //UserViewList.DataSource = dst;
            //UserViewList.DataBind();
            AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
            if (AspNetPager1.RecordCount != 0)
            {
                labNoRecord.Visible = false;
                AspNetPager1.Visible = true;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dst.Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                UnitViewList.DataSource = pds;
                UnitViewList.DataBind();
            }
            else
            {
                UnitViewList.DataSource = null;
                UnitViewList.DataBind();
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
            }
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            UnitListBind(this.txtUnitName.Text.Trim(), this.drpG.SelectedValue.ToString());
        }

        protected void butDelet_Click(object sender, EventArgs e)
        {
            if (hiddenSelected.Value.Trim().Length > 0 && hiddenSelected.Value.Trim() != ",")
            {
                string[] arrUnitID = hiddenSelected.Value.Trim().Split(new char[] { ',' });
                foreach (string unitID in arrUnitID)
                {
                    if (unitID.Trim().Length > 0)
                    {
                        UnitClass.DeleteUnitByID(unitID);
                    }
                }
                UnitListBind(this.txtUnitName.Text.Trim(), this.drpG.SelectedValue.ToString());
            }
            
        }
        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            UnitListBind(this.txtUnitName.Text.Trim(), this.drpG.SelectedValue.ToString());
        }

        public string getLevel(string LevelID)
        {
            string returnvalue = string.Empty;
            switch (LevelID)
            {
                case "1":
                    returnvalue = "一级";
                    break;
                case "2":
                    returnvalue = "二级";
                    break;
                case "3":
                    returnvalue = "三级";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

        public string getStatus(string status)
        {
            string returnvalue = string.Empty;
            switch (status)
            {
                case "A":
                    returnvalue = "激活";
                    break;
                case "D":
                    returnvalue = "<font color='red'>未激活</font>";
                    break;
                default:
                    break;
            }

            return returnvalue;
        }

    }
}
